require('../runtime/jaba');

/*
** This is a NodeJS command line utility
*/
rl = require('readline');

BatchJS = require('./batchjs');
compiler = require('./compiler');

// Require all files, for debugger to set breakepoints
require('./compiler');

runFile = function(file) {
	BatchJS.run(file);
}

runSource = function(source) {
	BatchJS.runSource(source);
}

compileSource = function(source) {
	process.stdout.write(compiler.compile(source));
}

exports.run = function() {
	var file;

	// Make NodeJS believe that the script is executed by the BatchJS binary
	process.execPath = require.main.filename;

	if( process.argv[2] ) {
		if( process.argv[2] == "-rs" ) {
			return runSource(process.argv[3]);
		}
		else if( process.argv[2] == "-cs" ) {
			return compileSource(process.argv[3]);
		}
		else {
			return runFile(process.argv[2]);
		}
	}

	var i = rl.createInterface(process.stdin, process.stdout, null);
	i.prompt();
	i.on("line", function(file) {
		
		if( file == "exit" ) {
			i.close();
			process.stdin.destroy();
			return;
		}

		runFile(file);
	});
};